﻿using EMS.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace EMS.ViewModel
{
    /// <summary>
    /// 所有物资类型
    /// </summary>
    public class CargoTypeViewModel : ViewModelBase
    {
        //动态数据集合类型，继承自 Collection<T>，并实现 INotifyCollectionChanged 和 INotifyPropertyChanged 接口
        //存储集合实例，初始化为空集合。
        private List<CargoType>  cargoTypes= new List<CargoType>();
        //封装私有字段，通过 get 提供外部访问；set 中调用 RaisePropertyChanged() 通知绑定目标属性值已更新
        private CargoType newCargoType = new CargoType();
        
        public CargoType NewCargoType
        {
            get => newCargoType;
            set {  newCargoType = value; RaisePropertyChanged(); }
        }
        public List<CargoType> CargoTypes 
        {
            get => cargoTypes; 
            set 
            { 
                cargoTypes = value;
                RaisePropertyChanged();// 触发属性变更通知
            }
        }

        // 删除
        private CargoType selectedCargoType;
        public CargoType SelectedCargoType
        {
            get => selectedCargoType;
            set { selectedCargoType = value; RaisePropertyChanged(); }
        }

        // 刷新
        public void Refresh()
        {
            CargoTypes = new CargoTypeProvider().Select();
        }

        private void Add()
        {
            try
            {
                // 自动赋值插入日期
                if (NewCargoType.InsertDate == default)
                    NewCargoType.InsertDate = DateTime.Now;

                var provider = new CargoTypeProvider();
                if (provider.Insert(NewCargoType) > 0)
                {
                 
                    Refresh();
                    NewCargoType = new CargoType();
                }
                else
                {
                    // 可加提示：插入失败
                    System.Diagnostics.Debug.WriteLine("添加物资类型失败，请检查输入数据是否完整。");
                }
            }
            catch (Exception ex)
            {
                // 这里可以用日志或弹窗显示异常信息
                System.Diagnostics.Debug.WriteLine("添加异常: " + ex.Message);
            }
        }

        private void Delete()
        {
            try
            {
                var provider = new CargoTypeProvider();
                //生成一个 IEnumerable<CargoType> 序列，仅包含所有 IsSelected == true 的对象。
                var toDelete = CargoTypes.Where(x => x.IsSelected).ToList();
                if (toDelete.Count == 0)
                {
                    System.Diagnostics.Debug.WriteLine("未勾选要删除的物资类型");
                    return;
                }
                foreach (var item in toDelete)//遍历选中的物资类型
                {
                    // 删除每个选中的物资类型
                    provider.Delete(item);
                }
             
                Refresh();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("删除异常: " + ex.Message);
            }
        }
        public void Update()
        {
            try
            {
                if (SelectedCargoType != null)
                {
                    var provider = new CargoTypeProvider();
                    if(provider.Update(SelectedCargoType)>0)
                    {
                        Refresh();
                    }
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("未选择要修改的物资类型");
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("修改异常: " + ex.Message);
            }
        }

        // ICommand 接口用于定义命令，允许 ViewModel 响应用户操作
        public ICommand RefreshCommand { get; }
        public ICommand AddCommand { get; }
        public ICommand DeleteCommand { get; }

        public ICommand UpdateCommand { get; }

        public CargoTypeViewModel()// 构造函数，初始化数据和命令，让 ViewModel 准备好响应用户操作，xaml调用一次
        {
            cargoTypes = new CargoTypeProvider().Select();
            RefreshCommand = new RelayCommand(Refresh);

            AddCommand = new RelayCommand(Add);
            DeleteCommand = new RelayCommand(Delete);
            UpdateCommand = new RelayCommand(Update);
        }


    }
}
